Thread: Is {0,} okay?

  1. #16
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Hmm... I see. So the comma would say set every element before the last comma to whatever specified and everything after it to zero?

    As in:

    Code:
    int array[5] = {2,}
    
    // Is the same as
    
    int array[5] = {2,0,0,0,0}
    
    // ?
    I guess that makes sense, but I still say it's not that explicit.
    Sent from my iPadŽ

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's exactly the same with or without the comma.

    [edit]
    The last one, I mean.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Because you only gave one value during initialization here:
    Code:
    int array[20] = {2,};
    If you did this instead:
    Code:
    int array[20] = {2,35,86,14};
    Then it would show:
    2
    35
    86
    14
    0
    0
    .
    .
    .
    0
    0
    If you understand what you're doing, you're not learning anything.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by SlyMaelstrom
    Hmm... I see. So the comma would say set every element before the last comma to whatever specified and everything after it to zero?

    As in:

    Code:
    int array[5] = {2,}
    
    // Is the same as
    
    int array[5] = {2,0,0,0,0}
    
    // ?
    I guess that makes sense, but I still say it's not that explicit.
    It's also the same as
    Code:
    int array[5] = {2}
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by SlyMaelstrom
    Hmm... I see. So the comma would say set every element before the last comma to whatever specified and everything after it to zero?

    As in:

    Code:
    int array[5] = {2,}
    
    // Is the same as
    
    int array[5] = {2,0,0,0,0}
    
    // ?
    I guess that makes sense, but I still say it's not that explicit.
    Not the comma, just the fact that it contains at least one initialised value. Recall itsme86's post above from the standard:
    21 If there are fewer initializers in a brace-enclosed list than there are elements or members
    of an aggregate, or fewer characters in a string literal used to initialize an array of known
    size than there are elements in the array, the remainder of the aggregate shall be
    initialized implicitly the same as objects that have static storage duration.
    And objects of static storage duration are initialised to zeroes (NULL in the case of pointers). The comma is purely optional:
    Code:
    int array[5] = { 2 };
    int array[5] = { 2, };

  6. #21
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The comma means nothing.
    Code:
    int a[5] = {2,};
    is exactly equivalent to:
    Code:
    int a[5] = {2};
    They both mean the exact same thing to the compiler. The final comma is just optional. I'm guessing because it's easier to write a script that spits out a bunch of values followed by a comma rather than have to try to figure out which is the last value so you can omit the comma.
    If you understand what you're doing, you're not learning anything.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The comma has no effect. Period. It does nothing. The standard states, as I've just stated, that any elements you don't initialize, provided you have actually initialized at least one, will be set to zero.
    Code:
    int array[5] = { 1 };
    This sets array[ 0 ] to 1, and since I haven't initialized any of the others, they get set to 0.
    Code:
    int array[5];
    None of these get initialized to anything. They have whatever was in memory. No initialization is done.
    Code:
    int array[5] = { 0, 1 };
    The first element I manually set to zero, the next I set to 1, and the rest are initialized automatically to zero.

    It doesn't matter if there's a trailing comma. It has no effect. The comma does nothing.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Hmm, ok.

    I was thinking:

    Code:
    int array[5] = { 2 } // would initialize every element to 2. 
    // Isn't there a simple way to do that similar to this?
    Sent from my iPadŽ

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's what I've been trying to find out (and Sly refuses to believe is "explicit").

    Thanks.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SlyMaelstrom
    Hmm, ok.

    I was thinking:

    Code:
    int array[5] = { 2 } // would initialize every element to 2. 
    // Isn't there a simple way to do that similar to this?
    Sure, use memset.

    The reason you were thinking it filled them to 2, is because we're always doing this:
    Code:
    char array[BUFSIZ] = {0};
    Which gives us a nicely zeroed array.


    Quzah.
    Last edited by quzah; 11-21-2005 at 06:48 PM.
    Hope is the first step on the road to disappointment.

  11. #26
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, that's exactly right.

    I've actually never had a situation where I've had to initialize a whole array to anything but 0.
    Sent from my iPadŽ

  12. #27
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The tricky one is automagically initializing a sequentially numbered array. Gone be the days of:
    Code:
    {
      int array[100], i;
    
      for(i = 0;i < 100;++i)
        array[i] = i;
    }
    If you figure that one out let me know
    If you understand what you're doing, you're not learning anything.

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    perl -e "foreach(0..100){print\"$_, \"}"
    That might help.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #29
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by quzah
    Sure, use memset.
    memset() won't initialize the int array correctly.

  15. #30
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by Ancient Dragon
    memset() won't initialize the int array correctly.

    Why not?

Popular pages Recent additions subscribe to a feed